﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using webapi;
using webapi.Models;
using System.Data;
using webapi.Entity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace webapi.Controllers
{
    public class UserController : ApiController
    {
      //根据id获取学生信息
      [HttpGet]
      public IHttpActionResult getStudentInfo(string id)
      {
        Student student = new Student();
        DataRow dr = UserService.getStudentById(id);
        student.user_id = (int)dr[0];
        student.name = dr[1].ToString();
        student.email = dr[2].ToString();
        student.class_id = dr[3].ToString();
        student.grade = dr[4].ToString();
        student.class_name = dr[5].ToString();
        student.subject = dr[6].ToString();
        student.choose_num = int.Parse("0" + dr[7]);
        student.chose_num = UserService.getStudentChoseNumById(id);
        return Json<dynamic>(new { code=0, msg=true, student});
      }

      //获取全部用户
      [HttpGet]
      public IHttpActionResult getUser(int curr, int limit)
      {
        List<User> users = new List<User>();
        DataRowCollection dr = UserService.getAllUser();
        int j = curr - 1;
        int count = dr.Count;
        int len = count;
        if (count > limit)
        {
          int z = count - (j * limit);
          len = z >= limit ? limit : z;
        }
        for (int i = j; i < len + j; i++)
        {
          users.Add(new User
          {
            user_id = (int)dr[i][1],
            name = dr[i][3].ToString().Trim(),
            //email = dr[i][4].ToString().Trim(),
            type = (int)dr[i][5]
          });
        }
        return Json<dynamic>(new { code = 0, count, data = users });
      }

      //增加用户
      [HttpPost]
      public IHttpActionResult addUser([FromBody] JObject obj)
      {
        User user = JsonConvert.DeserializeObject<User>(obj["user"].ToString());
        string pwd = obj["pwd"].ToString();
        int i = UserService.addUser(user, pwd);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg });
      }

      //修改用户
      [HttpPost]
      public IHttpActionResult updateUser([FromBody] JObject obj)
      {
        User user = JsonConvert.DeserializeObject<User>(obj["user"].ToString());
        int i = UserService.updateUser(user);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg });
      }

      //修改密码
      [HttpPost]
      public IHttpActionResult updateUserOfPwd([FromBody] JObject obj)
      {
        string id = obj["user_id"].ToString();
        string pwd = obj["pwd"].ToString();
        int i = UserService.updateUserOfPwd(id, pwd);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg });
      }

      //删除用户
      [HttpGet]
      public IHttpActionResult deleteUser(string id)
      {
        int i = UserService.deleteUser(id);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg });
      }

      //重置密码
      public IHttpActionResult resetUser(string id)
      {
        int i = UserService.resetUser(id);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg });
      }
    }
}
